package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.SoulQuestionOptionMapper;
import com.tanhua.model.domain.SoulQuestionOption;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SoulQuestionOptionApiImpl implements SoulQuestionOptionApi {

    @Autowired
    private SoulQuestionOptionMapper soulQuestionOptionMapper;

    @Override
    public List<SoulQuestionOption> findByQuestionId(Integer questionId) {
        LambdaQueryWrapper<SoulQuestionOption> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SoulQuestionOption::getQuestionId, questionId);
        return soulQuestionOptionMapper.selectList(lqw);
    }

    @Override
    public Integer findByIds(List<Long> ids) {
        LambdaQueryWrapper<SoulQuestionOption> lqw = new LambdaQueryWrapper<>();
        lqw.in(SoulQuestionOption::getId, ids);
        List<SoulQuestionOption> options = soulQuestionOptionMapper.selectList(lqw);
        Integer total = 0;
        List<Integer> scores = CollUtil.getFieldValues(options, "score", Integer.class);
        for (Integer score : scores) {
            total += score;
        }
        return total;
    }
}
